package com.itheima.web.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.ums.MenuService;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.ums.UmsMenu;
import com.itheima.vo.ums.UmsMenuTree;
import com.itheima.web.mapper.ums.MenuMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class MenuServiceImpl extends ServiceImpl<MenuMapper, UmsMenu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuService menuService;

    /**
     * 分页查询
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultData listParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<UmsMenu> infoPage=new Page<>(pageNum,pageSize);
        //条件构造器
        LambdaQueryWrapper<UmsMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(parentId != null, UmsMenu::getParentId,parentId);
        this.page(infoPage,lqw);
        //return ResultData.ok("操作成功", PageResult.init(infoPage.getCurrent(),pageNum.longValue(),pageSize.longValue(),infoPage.getRecords()));
        PageResult result = PageResult.getPage(infoPage);
        return ResultData.ok("",result);
    }

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    @Override
    public List<UmsMenuTree> treeList() {
        //获取全部menu对象---不分级
        List<UmsMenu> allMenu = menuService.findAllMenu();
        //将UmsMenu全copy进UmsMenuTreeList中
        List<UmsMenuTree> umsMenuTreeList = allMenu.stream().map(menu -> {
            UmsMenuTree umsMenuTree = new UmsMenuTree();
            BeanUtils.copyProperties(menu, umsMenuTree);
            return umsMenuTree;
        }).collect(Collectors.toList());

        //菜单只有两级找到0级将1级封装进去---无需递归
        List<UmsMenuTree> list = umsMenuTreeList.stream().filter(umsMenuTree ->
                umsMenuTree.getParentId() == 0L
        ).collect(Collectors.toList());

        list.stream().map(menuParent -> {
            List<UmsMenuTree> children = new ArrayList<>();
            for (UmsMenuTree umsMenuSon : umsMenuTreeList) {
                if (umsMenuSon.getParentId() == menuParent.getId()) {
                    children.add(umsMenuSon);
                }
            }
            menuParent.setChildren(children);
            return menuParent;
        }).collect(Collectors.toList());
        System.out.println(list.toString());
        return list;
    }

    /**
     * 获取全部menu对象---不分级
     */
    @Override
    public List<UmsMenu> findAllMenu() {
        LambdaQueryWrapper<UmsMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(UmsMenu::getCreateTime);
        List<UmsMenu> umsMenus = menuMapper.selectList(lambdaQueryWrapper);
        return umsMenus;
    }

    /**
     * 修改菜单显示状态
     * @param id
     * @param hidden
     * @return
     */
    @Override
    public void updateHidden(Long id, Integer hidden) {
        UmsMenu umsMenu = menuMapper.selectById(id);
        umsMenu.setHidden(hidden);
        menuMapper.updateById(umsMenu);
    }
}
